package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.controller.Gcontroller;
import com.controller.IController;
import com.controller.PController;

/** 
* @author  Jrxsr
* @version 2018年11月1日
* 
*/
public class HtmlFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("初始化Filter");
		//Jedis jedis = new Jedis("123.206.76.239",6379);
		//jedis.auth("121380");
		//System.out.println(jedis.ping());//"http://192.168.10.185:8088/ThreeSSM/"
		IController.setServerIP("http://39.106.32.162:8081/ThreeSSM/"); //"http://192.168.0.108:8088/ThreeSSM/"  jedis.get("serverIP")     "http://101.132.139.180/ThreeSSM/"
		Gcontroller.setServerIP("http://39.106.32.162:8081/ThreeSSM/");
		PController.setServerIP("http://39.106.32.162:8081/ThreeSSM/");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("FilterDemo02执行前！！！");
		// 由于拦截的是.html页面 所以不予放行
		//chain.doFilter(request, response);  //让目标资源执行，放行
		// 如果session中不存在登录者实体，则弹出框提示重新登录
		// 设置request和response的字符集，防止乱码
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		StringBuilder builder = new StringBuilder();
		builder.append("<script type=\"text/javascript\">");
		builder.append("alert('您好，请先登录！');");
		builder.append("location.href='login';");
		builder.append("</script>");
		out.print(builder.toString());
	}

	@Override
	public void destroy() {
		System.out.println("Filter销毁了");
	}

}
